/**
 * Form Layout by Dagny.
 * Functional coding by Dagny.
 */

package Forms;

import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Vector;

import Classes.Agencies;
import Classes.Agents;

import javax.swing.BorderFactory;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListModel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.border.BevelBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import teComponents.*;
import Classes.Validate;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class AgentsForm  extends teComponents.teJinternalFrame 
{
	//private properties
	private teJList lstAll;
	private teJButton btnGo;
	private teJLabel lblsearchAgency;
	private teJComboBox cbosearchAgency;
	private teJLabel lblsearchPosition;
	private teJComboBox cbosearchPosition;
	private teJLabel lblsearchName;
	private teJPanel pnlSearch;
	private teJLabel lblSelectedPanel;
	private teJLabel lblAgendId;
	private teJTextField txtAgentId;
	private teJLabel lblEmail;
	private teJTextField txtAgentEmail;
	private JPanel pnlSelected;
	private teJButton btnAdd;
	private JScrollPane jScrollPane1;
	private teJComboBox cboAgency;
	private teJComboBox cboPosition;
	private teJLabel lblLastName;
	private teJTextField txtLastName;
	private teJLabel lblMiddle;
	private teJTextField txtMiddle;
	private teJLabel lblFirstName;
	private teJTextField txtFirstName;
	private teJLabel lblAgency;
	private teJButton btnUpdate;
	private teJButton btnClear;
	private teJButton btnDelete;
	private teJLabel lblSearch;
	private teJTextField txtsearchName;
	private teJLabel lblAll;
	private teJLabel lblPhone;
	private teJTextField txtPhone;
	private teJLabel lblPosition;
	//create a generic Agent for use loading the combo boxes
	private Agents genericAgent = new Agents();
	
	//main just to view the form and test it
	//comment out once we have it joined with the rest of the application
	public static void main(String[] args) 
	{
		SwingUtilities.invokeLater(new Runnable() 
		{
			public void run() 
			{
				AgentsForm inst = new AgentsForm();
				//inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	
	//public constructor
	public AgentsForm() 
	{
		super();
		//load the GUI builder
		initGUI();
	}
	public AgentsForm(teJFrame caller)
	{
		super(caller);
		//load the GUI builder
		initGUI();
	}
	//GUI Builder
	private void initGUI() 
	{
		
		//default to enter into combo boxes until the refresh happens at the end of the GUI creation:
		String[] defaultComboBoxString = {"choice"};
		ComboBoxModel cboDefaultLoad = 
			new DefaultComboBoxModel(defaultComboBoxString);
		
		try {
			{
				this.setTitle("Maintain Agents");
				{
					jScrollPane1 = new JScrollPane();
					getContentPane().add(jScrollPane1);
					jScrollPane1.setBounds(12, 47, 455, 305);
					{
						lstAll = new teJList();
						jScrollPane1.setViewportView(lstAll);
						lstAll.setBounds(12, 47, 455, 305);
						refreshAgentsList();
						lstAll.addListSelectionListener(new ListSelectionListener() 
						{
							
							@Override
							public void valueChanged(ListSelectionEvent e) 
							{
								//if the selection is not null
								if (lstAll.getSelectedIndex() != -1)
								{
									//when the selection in the list box changes, populate the 
									//information for the agent in the Your Selection Panel
									//first create a new agent to hold the information
									Agents selectedAgent = (Agents)lstAll.getSelectedValue();
									//then grab the agent id from that new agent
									int agentid = selectedAgent.getAgentId();
									// using the loadAgentInformation function in Agents,
									selectedAgent.loadAgentInformation(agentid);
									ArrayList<String> agentInfo = selectedAgent.loadAgentInformation(agentid);
									//grab each element of the agentInfo array and put it into the various fields:
									txtAgentId.setText((String)(agentInfo.get(0)));
									txtFirstName.setText(agentInfo.get(1));
									txtMiddle.setText(agentInfo.get(2));
									txtLastName.setText(agentInfo.get(3));
									txtPhone.setText(agentInfo.get(4));
									txtAgentEmail.setText(agentInfo.get(5));
									cboPosition.setSelectedItem((String)(agentInfo.get(6)));
									//get the agency id from the agent (it's in string form):
									int agencyid = Integer.parseInt(agentInfo.get(7));

									//go through each agency in the cboAgency box
									//until the one matching the id is found
									//count items in combo box:
									int agencyNum = cboAgency.getItemCount();
									for (int a=0; a < agencyNum; a++)
									{
										//get the test agency id
										//int testAgencyId = ((Agencies)(cboAgency.getItemAt(a))).getAgencyId();
										Agencies testAgency = (Agencies) cboAgency.getItemAt(a);
										int testAgencyId = testAgency.getAgencyId();
										if (testAgencyId == agencyid)
										{
											//set the selection of the cbo to the index a
											cboAgency.setSelectedIndex(a);
										}
									}
									
									//disable the Add button, the Search button and the Agent ID field
									btnAdd.setEnabled(false);
									btnGo.setEnabled(false);
									txtAgentId.setEditable(false);
									//enable the Update and Delete Buttons
									btnDelete.setEnabled(true);
									btnUpdate.setEnabled(true);
								}
							}
						});
						
						
					}
				}
			}
			{
				this.setSize(914, 382);
				this.setPreferredSize(new java.awt.Dimension(912, 428));
				this.setBounds(0, 0, 912, 428);
			}
			{
				lblAll = new teJLabel();
				getContentPane().add(lblAll);
				lblAll.setText("Agents");
				lblAll.setBounds(12, 27, 80, 14);
				lblAll.setHorizontalAlignment(SwingConstants.LEFT);
			}
			{
				lblSearch = new teJLabel();
				getContentPane().add(lblSearch);
				lblSearch.setText("Search for: ");
				lblSearch.setBounds(483, 27, 110, 14);
				lblSearch.setHorizontalAlignment(SwingConstants.LEFT);
			}
			{
				btnAdd = new teJButton();
				getContentPane().add(btnAdd);
				btnAdd.setText("Add");
				btnAdd.setBounds(12, 358, 100, 25);
				btnAdd.setToolTipText("Add a new Agent to the database, based on the information in the 'Your Selection' form on the right.");
				btnAdd.addMouseListener(new MouseAdapter() 
				{
					public void mouseClicked(MouseEvent e)
					{
						if (validateAgentForm())
						{
							//create an instance of Agents
							Agents addAgent = new Agents();
							
							int agentid = addAgent.getNextNewAgentId();
							String first = txtFirstName.getText();
							String middle = txtMiddle.getText();
							String last = txtLastName.getText();
							String unformattedPhone = txtPhone.getText().replaceAll("[^0-9]", "");
							//format the phone
							String phone = "(" + unformattedPhone.substring(0, 3) + 
											") " + unformattedPhone.substring(3, 6) + "-" + unformattedPhone.substring(6);
							String email = txtAgentEmail.getText();
							String position = cboPosition.getSelectedItem().toString();
							
							//cast the item in the cboAgency box to an Agency
							Agencies selectedAgency = (Agencies) cboAgency.getSelectedItem();
							//get the agency id from it
							int agencyid = selectedAgency.getAgencyId();
																					
							//call the add method from Agents
							addAgent.addAgent(agentid, first, middle, last, phone, email, position, agencyid);
							
							String username;
							//create the information to pass to the addAgentLogin method:
							//create the username:
							try
							{
								//grab the first four letters of their first name
								username = first.substring(0, 4) + "" + agentid;
							}
							catch (Exception ex) 
							{
								ex.printStackTrace();
								//if there aren't four letters to grab (name is three or less characters)
								//then grab whole name
								username = first + "" + agentid;
							}
							
							//call the add to agent login method from Agents
							addAgent.addAgentLogin(username, agentid, "1234");
							
							//refresh the Agents List Box
							refreshAgentsList();
							
							//clear the Selection boxes
							clearSelectionPanel();
							
							//refresh the drop down boxes
							refreshDropDownBoxes();
						}
					}
				});
			}
			{
				btnDelete = new teJButton();
				getContentPane().add(btnDelete);
				btnDelete.setText("Delete");
				btnDelete.setBounds(128, 358, 100, 25);
				btnDelete.setEnabled(false);
				btnDelete.setToolTipText("Delete the selected Agent from the database.");
				btnDelete.addMouseListener(new MouseAdapter() 
				{
					public void mouseClicked(MouseEvent e)
					{
						//create an instance of Agents
						Agents deleteAgent = new Agents();
						
						int agentid = Integer.parseInt(txtAgentId.getText());

						//delete the agentlogin for the agent, so there
						//won't be any child records:
						deleteAgent.deleteAgentLogin(agentid);
						
						//clear any customers who are attached to this agent
						//so there aren't any children. We have the ability
						//to search for any customer with a null agent id
						//so they can be assigned to an agent.
						deleteAgent.clearAgentIdInCustomerTable(agentid);
						
						//call the delete method from Agents
						deleteAgent.deleteAgent(agentid);
						
						//refresh the Agents List Box
						refreshAgentsList();
						
						//clear the Selection boxes
						clearSelectionPanel();
						
						//refresh the drop down boxes
						refreshDropDownBoxes();
						
						//enable the add and search buttons again
						btnAdd.setEnabled(true);
						btnGo.setEnabled(true);
					}
				});
			}
			{
				btnClear = new teJButton();
				getContentPane().add(btnClear);
				btnClear.setText("Clear");
				btnClear.setBounds(365, 358, 100, 25);
				btnClear.setToolTipText("Clears all the information in the forms and refreshes the Agent list with all agents in the database.");
				btnClear.addMouseListener(new MouseAdapter() 
				{
					public void mouseClicked(MouseEvent e)
					{
						//clear the list box
						lstAll.clearSelection();
						//reload the list box with all agents
						refreshAgentsList();
						
						//clear all the text boxes and combo boxes
						clearSelectionPanel();
						clearSearchPanel();
						
						//disable the add, delete and update buttons
						btnAdd.setEnabled(true);
						btnDelete.setEnabled(false);
						btnUpdate.setEnabled(false);
						btnGo.setEnabled(true);
					}
				});
			}
			{
				btnUpdate = new teJButton();
				getContentPane().add(btnUpdate);
				btnUpdate.setText("Update");
				btnUpdate.setBounds(247, 358, 100, 25);
				btnUpdate.setEnabled(false);
				btnUpdate.setToolTipText("Update the selected Agent in the database.");
				btnUpdate.addMouseListener(new MouseAdapter() 
				{
					public void mouseClicked(MouseEvent e)
					{
						if (validateAgentForm())
						{
							//create an instance of Agents
							Agents updateAgent = new Agents();
							
							int agentid = Integer.parseInt(txtAgentId.getText());
							String first = txtFirstName.getText();
							String middle = txtMiddle.getText();
							String last = txtLastName.getText();							
							String unformattedPhone = txtPhone.getText().replaceAll("[^0-9]", "");
							//format the phone							
							String phone = "(" + unformattedPhone.substring(0, 3) + 
											") " + unformattedPhone.substring(3, 6) + "-" + unformattedPhone.substring(6);
							String email = txtAgentEmail.getText();
							String position = cboPosition.getSelectedItem().toString();
							
							//cast the item in the cboAgency box to an Agency
							Agencies selectedAgency = (Agencies) cboAgency.getSelectedItem();
							//get the agency id from it
							int agencyid = selectedAgency.getAgencyId();
																			
							
							//call the update method from Agents
							updateAgent.updateAgent(agentid, first, middle, last, phone, email, position, agencyid);
							
							//refresh the Agents List Box
							refreshAgentsList();
							
							//clear the Selection boxes
							clearSelectionPanel();
							
							//refresh the drop down boxes
							refreshDropDownBoxes();
							
							//enable the add and search buttons again
							btnAdd.setEnabled(true);
							btnGo.setEnabled(true);
						}
					}
				});
			}
			{
				pnlSelected = new teJPanel();
				getContentPane().add(pnlSelected);
				pnlSelected.setBounds(479, 190, 415, 195);
				pnlSelected.setLayout(null);
				{
					txtAgentId = new teJTextField();
					pnlSelected.add(txtAgentId);
					txtAgentId.setBounds(8, 27, 82, 18);
					txtAgentId.setEditable(false);
				}
				{
					lblAgendId = new teJLabel();
					pnlSelected.add(lblAgendId);
					lblAgendId.setText("Agent ID:");
					lblAgendId.setBounds(8, 13, 82, 14);
					lblAgendId.setHorizontalAlignment(SwingConstants.LEFT);
				}
				{
					lblEmail = new teJLabel();
					pnlSelected.add(lblEmail);
					lblEmail.setText("Email:");
					lblEmail.setBounds(102, 13, 123, 14);
					lblEmail.setHorizontalAlignment(SwingConstants.LEFT);
				}
				{
					txtAgentEmail = new teJTextField();
					pnlSelected.add(txtAgentEmail);
					txtAgentEmail.setBounds(102, 27, 300, 18);
				}
				{
					txtFirstName = new teJTextField();
					pnlSelected.add(txtFirstName);
					txtFirstName.setBounds(8, 70, 121, 18);
				}
				{
					lblFirstName = new teJLabel();
					pnlSelected.add(lblFirstName);
					lblFirstName.setText("First Name:");
					lblFirstName.setBounds(8, 55, 76, 14);
					lblFirstName.setHorizontalAlignment(SwingConstants.LEFT);
				}
				{
					txtMiddle = new teJTextField();
					pnlSelected.add(txtMiddle);
					txtMiddle.setBounds(141, 70, 21, 18);
				}
				{
					lblMiddle = new teJLabel();
					pnlSelected.add(lblMiddle);
					lblMiddle.setText("M.I.");
					lblMiddle.setBounds(138, 55, 28, 14);
					lblMiddle.setHorizontalAlignment(SwingConstants.LEFT);
				}
				{
					txtLastName = new teJTextField();
					pnlSelected.add(txtLastName);
					txtLastName.setBounds(174, 70, 228, 18);
				}
				{
					lblLastName = new teJLabel();
					pnlSelected.add(lblLastName);
					lblLastName.setText("Last Name:");
					lblLastName.setBounds(175, 55, 79, 14);
					lblLastName.setHorizontalAlignment(SwingConstants.LEFT);
				}
				{
					txtPhone = new teJTextField();
					pnlSelected.add(txtPhone);
					txtPhone.setBounds(8, 117, 118, 18);
				}
				{
					lblPhone = new teJLabel();
					pnlSelected.add(lblPhone);
					lblPhone.setText("Phone:");
					lblPhone.setBounds(8, 94, 49, 14);
					lblPhone.setHorizontalAlignment(SwingConstants.LEFT);
				}
				{
					lblPosition = new teJLabel();
					pnlSelected.add(lblPosition);
					lblPosition.setText("Position:");
					lblPosition.setBounds(141, 94, 62, 14);
					lblPosition.setHorizontalAlignment(SwingConstants.LEFT);
				}
				{
					cboPosition = new teJComboBox();
					pnlSelected.add(cboPosition);
					cboPosition.setModel(cboDefaultLoad);
					cboPosition.setBounds(140, 108, 262, 28);
				}
				{
					lblAgency = new teJLabel();
					pnlSelected.add(lblAgency);
					lblAgency.setText("Agency:");
					lblAgency.setBounds(8, 141, 80, 14);
					lblAgency.setHorizontalAlignment(SwingConstants.LEFT);
				}
				{
					cboAgency = new teJComboBox();
					pnlSelected.add(cboAgency);
					cboAgency.setModel(cboDefaultLoad);
					cboAgency.setBounds(8, 156, 394, 28);
				}
			}
			{
				lblSelectedPanel = new teJLabel();
				getContentPane().add(lblSelectedPanel);
				lblSelectedPanel.setText("Your Selection:");
				lblSelectedPanel.setBounds(479, 172, 101, 14);
				lblSelectedPanel.setHorizontalAlignment(SwingConstants.LEFT);
			}
			{
				pnlSearch = new teJPanel();
				getContentPane().add(pnlSearch);
				pnlSearch.setBounds(483, 47, 415, 112);
				pnlSearch.setLayout(null);
				{
					txtsearchName = new teJTextField();
					pnlSearch.add(txtsearchName);
					txtsearchName.setBounds(87, 13, 238, 21);
				}
				{
					btnGo = new teJButton();
					pnlSearch.add(btnGo);
					btnGo.setText("Search");
					btnGo.setBounds(331, 13, 77, 87);
					btnGo.setToolTipText("Searches the Agents database for agents that match the parameters in the search panel.");
					btnGo.addMouseListener(new MouseAdapter() 
					{
						public void mouseClicked(MouseEvent e)
						{
							//clear the current list
							lstAll.clearSelection();
							
							//disable the add, delete and update buttons
							btnAdd.setEnabled(false);
							btnDelete.setEnabled(false);
							btnUpdate.setEnabled(false);
							
							//create the SQL statement to search with
							String searchSQL = "SELECT * FROM agents WHERE (1 = 1) ";
							//check to see if any of the other parameters are there
							if (txtsearchName.getText() != "")
							{
								searchSQL += " AND (UPPER(AgtFirstName) Like '%" + txtsearchName.getText().toUpperCase() + "%'";
								searchSQL += " OR UPPER(AgtLastName) Like '%" + txtsearchName.getText().toUpperCase() + "%')";
							}
							if (cbosearchPosition.getSelectedItem().toString() != "- - Select a Position - -")
							{
								searchSQL += " AND (AgtPosition ='" + cbosearchPosition.getSelectedItem().toString() + "')";
							}
							
							//cast the item in the cboAgency box to an Agency
							Agencies selectedAgency = (Agencies) cbosearchAgency.getSelectedItem();
							//get the agency id from it
							int agencyid = selectedAgency.getAgencyId();
							
							//if the combo box says "- - Select an Agency - -", it's the default agency 
							//created with an id of 0, so check to make sure it's not zero
							if (agencyid != 0)
							{
								 searchSQL += " AND (AgencyId = " + agencyid + ")";
								 //searchSQL += " AND (AgencyId = 1)";
							}
							searchSQL += " ORDER BY AgtLastName";
							
							//test resulting statement
							//System.out.println(searchSQL);
							
							//refresh the Agent List based on the SQL query
							refreshAgentsList(searchSQL);
						}
					});
				}
				{
					lblsearchName = new teJLabel();
					pnlSearch.add(lblsearchName);
					lblsearchName.setText("Name:");
					lblsearchName.setBounds(33, 20, 46, 14);
				}
				{
					lblsearchPosition = new teJLabel();
					pnlSearch.add(lblsearchPosition);
					lblsearchPosition.setText("Position:");
					lblsearchPosition.setBounds(16, 51, 63, 14);
				}
				{
					cbosearchPosition = new teJComboBox();
					pnlSearch.add(cbosearchPosition);
					cbosearchPosition.setModel(cboDefaultLoad);
					cbosearchPosition.setBounds(86, 39, 239, 28);
				}
				{
					lblsearchAgency = new teJLabel();
					pnlSearch.add(lblsearchAgency);
					lblsearchAgency.setText("Agency:");
					lblsearchAgency.setBounds(24, 81, 55, 14);
				}
				{
					cbosearchAgency = new teJComboBox();
					pnlSearch.add(cbosearchAgency);
					cbosearchAgency.setModel(cboDefaultLoad);
					cbosearchAgency.setBounds(86, 73, 239, 28);
				}
				//refresh the drop down boxes
				refreshDropDownBoxes();
			}
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	private void clearSelectionPanel()
	{
		 Component [] compsSelectPanel = pnlSelected.getComponents();
		 for (Component comp : compsSelectPanel)
		 {
			if (comp instanceof teJTextField)
			 {
				 //clear the text field
				 ((teJTextField) comp).setText("");

			 }
			 else if (comp instanceof teJComboBox)
			 {
				 ((teJComboBox) comp).setSelectedIndex(0);
			 }
		 }
	}

	private void clearSearchPanel()
	{
		 Component [] compsSelectPanel = pnlSearch.getComponents();
		 for (Component comp : compsSelectPanel)
		 {
			if (comp instanceof teJTextField)
			 {
				 //clear the text field
				 ((teJTextField) comp).setText("");

			 }
			 else if (comp instanceof teJComboBox)
			 {
				 ((teJComboBox) comp).setSelectedIndex(0);
			 }
		 }
	}
	
	//refresh the drop down menus for the combo boxes whenever something changes in the database
	private void refreshDropDownBoxes()
	{
		//create a string ArrayList for loading the positions, then create the comboboxmodel
		ArrayList<String> searchPositionAr = genericAgent.loadPositions();
		String[] stringSearchPositionAr = searchPositionAr.toArray(new String[searchPositionAr.size()]);
		ComboBoxModel cbosearchPositionModel = 
			new DefaultComboBoxModel(stringSearchPositionAr);
		
		//create a string ArrayList for loading the positions, then create the comboboxmodel
		ArrayList<String> positionAr = genericAgent.loadPositions();
		String[] stringPositionAr = positionAr.toArray(new String[positionAr.size()]);
		ComboBoxModel cboPositionModel = 
			new DefaultComboBoxModel(stringPositionAr);
		
		//capturing just strings
		/*
		//create a string ArrayList for loading the agencies, then create the comboboxmodel
		ArrayList<String> agencyAr = genericAgent.loadAgencies();
		String[] stringAgencyAr = agencyAr.toArray(new String[agencyAr.size()]);
		ComboBoxModel cboAgencyModel = 
			new DefaultComboBoxModel(stringAgencyAr);

		//create a string ArrayList for loading the search agencies, then create the comboboxmodel
		ArrayList<String> searchAgencyAr = genericAgent.loadAgencies();
		String[] stringSearchAgencyAr = searchAgencyAr.toArray(new String[searchAgencyAr.size()]);
		ComboBoxModel cbosearchAgencyModel = 
			new DefaultComboBoxModel(stringSearchAgencyAr);
		 */
		
		//capturing agency as vectors instead of strings
		Agencies genericAgency = new Agencies();	
		Vector<Object> agenciesVector = genericAgency.agenciesVector("select * from Agencies");
		ComboBoxModel cboAgencyModel =
			new DefaultComboBoxModel(agenciesVector);
		
		Vector<Object> agenciesSearchVector = genericAgency.agenciesVector("select * from Agencies");
		ComboBoxModel cbosearchAgencyModel = 
			new DefaultComboBoxModel(agenciesSearchVector);
		
		//reset the values of the combo boxes:
		cbosearchPosition.setModel(cbosearchPositionModel);
		cbosearchAgency.setModel(cbosearchAgencyModel);
		cboPosition.setModel(cboPositionModel);
		cboAgency.setModel(cboAgencyModel);
		
	}
	//if no SQL search statement is passed through as a parameter,
	//the following refreshAgentsList method finds all Agents in the DB
	private void refreshAgentsList()
	{
		ArrayList<Agents> newAgentsArrayList = genericAgent.agentsAr("SELECT * FROM agents ORDER BY AgtLastName");
		Agents[] newAllAgents = genericAgent.createAgentArray(newAgentsArrayList);
		lstAll.clearSelection();
		
		//change colour of text to dark, dark brown #2b2003, rgb 43,31,2
		lstAll.setForeground(new java.awt.Color(43,31,2));
		
		//reset the colour of the selection background
		lstAll.setSelectionBackground(new java.awt.Color(2,234,231));
		//set focusable back to true
		lstAll.setFocusable(true);
		
		//refresh the list box
		ListModel newLstAllModel = 
			new DefaultComboBoxModel(newAllAgents);
		lstAll.setModel(newLstAllModel);
	}
	
	//overload the method refreshAgentsList to pass through a select parameter.
	//If no parameter passed through, the Select * From agents is used
	private void refreshAgentsList(String sqlSearch)
	{
		ArrayList<Agents> newAgentsArrayList = genericAgent.agentsAr(sqlSearch);
		Agents[] newAllAgents = genericAgent.createAgentArray(newAgentsArrayList);
		lstAll.clearSelection();
		
		//change colour of text to dark, dark brown #2b2003, rgb 43,31,2
		lstAll.setForeground(new java.awt.Color(43,31,2));
		
		//reset the colour of the selection background
		lstAll.setSelectionBackground(new java.awt.Color(2,234,231));
		//set focusable back to true
		lstAll.setFocusable(true);
		
		//reload the list box
		ListModel newLstAllModel = 
			new DefaultComboBoxModel(newAllAgents);
		lstAll.setModel(newLstAllModel);
	}
	
	private boolean validateAgentForm()
	{
		boolean result = true;
		ArrayList<String> errMsg = new ArrayList<String>();
		
		//testing email address field:
		//if the email field is empty
		if (txtAgentEmail.getText().equals(""))
		{
			errMsg.add("An email address must be supplied.");
			result = false;
		}
		//if the email isn't valid
		else if (!Validate.isValidEmail(txtAgentEmail.getText()))
		{
			errMsg.add("The email address format is invalid.");
			result = false;
		}
		
		//testing first name field:
		//if the first name field is empty
		if (txtFirstName.getText().equals(""))
		{
			errMsg.add("A first name must be supplied.");
			result = false;
		}
		//if the first name isn't valid text
		else if (!Validate.genericText(txtFirstName.getText()))
		{
			errMsg.add("The first name field can't contain a semicolon (;) or double dash (--).");
			result = false;
		}
		
		//testing last name field:
		//if the last name field is empty
		if (txtLastName.getText().equals(""))
		{
			errMsg.add("A last name must be supplied.");
			result = false;
		}
		//if the last name isn't valid text
		else if (!Validate.genericText(txtLastName.getText()))
		{
			errMsg.add("The last name field can't contain a semicolon (;) or double dash (--).");
			result = false;
		}
		
		//testing phone number field:
		//if the phone number field is empty
		if (txtPhone.getText().equals(""))
		{
			errMsg.add("A phone number must be supplied.");
			result = false;
		}
		//if the last name isn't valid text

		else if (!Validate.isValidPhoneFax(txtPhone.getText().replaceAll("[^0-9]", "")))
		{
			errMsg.add("The phone number must be 10 digits, no special characters.");
			result = false;
		}
		
		//testing position and agency fields:
		//if it's the default text
		if (cboPosition.getSelectedItem().toString().equals("- - Select a Position - -"))
		{
			errMsg.add("A position must be selected.");
			result = false;
		}
		//cast the selected item in the agency combo box to an Agency, then get the id
		//if it's "- - Select an Agency - -", it's the default agency created with an id of 0
		if (((Agencies)(cboAgency.getSelectedItem())).getAgencyId() == 0)
		//if (cboAgency.getSelectedItem().toString().equals("- - Select an Agency - -"))
		{
			errMsg.add("An agency must be selected.");
			result = false;
		}
		
		if (result == false)
		{
			//create an errorMsg Array to populate the lstAll list		
			int lengthOfErrMsg = errMsg.size();
			String [] errorArray = new String[lengthOfErrMsg];
			for (int e = 0; e <lengthOfErrMsg; e++)
			{
				errorArray[e] = errMsg.get(e);
			}
			
			//clear the lstAll
			lstAll.clearSelection();
			
			//change colour of text to red #c62E04, rgb 198,42,3
			lstAll.setForeground(new java.awt.Color(198,42,3));
			
			//set the selection colour to the background so 
			//it doesn't look like anything's getting selected
			lstAll.setSelectionBackground(lstAll.getBackground());
			//set focusable to false
			lstAll.setFocusable(false);
			
			//refresh the lstAll 
			ListModel newLstAllModel = 
				new DefaultComboBoxModel(errorArray);
			lstAll.setModel(newLstAllModel);
		}
		
		return result;
	}
}
